<?php

namespace IDock\Insurance\DBIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\DBIC\DBICEnums;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;

class VehicleModelResponse extends AllDBICResponse
{

    /**
     * @var VehicleModelPrinter $printer
     */
    public $printer = VehicleModelPrinter::class;

    public function printerInit(): Response
    {
        $modelList = $this->getRecords();

        if (!$modelList) {
            return $this;
        }

        foreach ($modelList as $datum) {

            $vehiclePrinter = new VehiclePrinter();
            $vehiclePrinter->vehicleModelCode = $datum['modelCode'] ?? '';
            $vehiclePrinter->brandName = $datum['modelName'] ?? '';
            $vehiclePrinter->vehicleExhaust = $datum['exaustScale'] ?? '';

            //进口车标识
            $vehicleType = $datum['countryCode'] ?? 'I0' ?: 'I0';
            $vehiclePrinter->vehicleImport = DBICEnums::$importFlag[$vehicleType];
            $vehiclePrinter->vehicleImportName = IDockEnums::$importFlag[$vehiclePrinter->vehicleImport];

            $vehiclePrinter->vehicleName = $datum['modelName'] ?? '';
            $vehiclePrinter->vehiclePrice = $datum['purchasePrice'] ?? '';
            $vehiclePrinter->vehiclePriceWithOutTax = $datum['purchasePrice'] ?? '';
            $vehiclePrinter->vehicleSeat = $datum['seatCount'] ?? '';
            $vehiclePrinter->remark = $datum['modelDesc'] ?? '';

            $vehiclePrinter->vehicleYear = $datum['carYear'] ?? '';

            $tonCount = $datum['tonCount'] ?? '0';
            if (!$tonCount) {
                $tonCount = '0';
            }else{
                $tonCount = $tonCount * 1000;
            }

            $vehiclePrinter->vehicleTonnage = $tonCount;
            if ($datum['completekerbMass'] ?? '') {
                $vehiclePrinter->wholeWeight = (string)$datum['completekerbMass'] * 1000;
            }

            //能源类型
            $powerTypeCode = $datum['powerTypeCode'] ?? '0';
            $vehiclePrinter->energyTypeCode = array_flip(DBICEnums::$energyType)[(string)($powerTypeCode)];
            $vehiclePrinter->energyTypeName = IDockEnums::$energyType[$vehiclePrinter->energyTypeCode];

            if ($powerTypeCode == '1' or $powerTypeCode == '3') {
                $vehiclePrinter->isNewEnergy = '1';
            }

            //缴税类型
            $taxReduceCode = $datum['hfCode'] ?? '0';
            $vehiclePrinter->taxReduceCode = DBICEnums::$taxReduceCode[$taxReduceCode];
            $vehiclePrinter->taxReduceName = IDockEnums::$taxReduceCode[$vehiclePrinter->taxReduceCode];
            $vehiclePrinter->item['platModelCode'] = $datum['platModelCode'] ?? '';

            $this->printer->setModelList($vehiclePrinter);
        }

        return $this;
    }
}
